import java.net.InetAddress

import cn.tecnova.utils.{ConfigHandler, ESUtils}
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.SearchHit
import org.elasticsearch.transport.client.PreBuiltTransportClient

/**
  * description:es测试
  **/
object ES {

  def main(args: Array[String]): Unit = {

    val settings = Settings.builder().put("cluster.name", "nova-application02").build()

    val client = new PreBuiltTransportClient(settings)
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost1), 9700))
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost2), ConfigHandler.esPort))
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost3), ConfigHandler.esPort))
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.17.22.17"), ConfigHandler.esPort))
//    val client = ESUtils.getEsClient()

    val builder = client.prepareSearch("ba_user_warning").setTypes("ba_user_warning")
    //      .setQuery(QueryBuilders.matchQuery("id","5d6c23ad-052b-44bb-ad51-ae984d2cf58b"))
    //        .setQuery(QueryBuilders.matchQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38"))
    //      .get()
    //      .setQuery(QueryBuilders.matchQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38")).get()

        val builder1 = QueryBuilders.matchPhraseQuery("id","42b05da4-ee11-448d-a6fd-ca6afd15d426")
//        val builder2 = QueryBuilders.matchPhraseQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38")
//    val builder2 = QueryBuilders.matchPhraseQuery("article_id", "001")

    val sr = builder.setQuery(QueryBuilders.boolQuery()
      .must(builder1)
//      .must(builder2)
    )
      .execute()
      .actionGet();
    val hits: Array[SearchHit] = sr.getHits.getHits

    //es中没有该记录
    if(hits.length==0){
      println("写到kafka")
    }


    for (r <- hits) {
      println(r.getSourceAsString)
      println(r.getId)

    }
    client.close()
  }

}
